package com.cdks.wuxiu.api.component.chuanglan;

import com.alibaba.fastjson.JSONObject;
import com.cdks.wuxiu.api.component.chuanglan.utils.*;
import com.cdks.wuxiu.common.config.ShanYanAndroidProps;
import com.cdks.wuxiu.common.config.ShanYanIOSProps;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class FlashMobileLoginUtil {

    public static String getFlashMobilePhone(String token, Integer branch) {
        //应用对应的闪验APPID
        String appId = "";
        //应用对应的闪验APPKEY
        String appKey = "";
        //手机号加解密方式 0 AES 1 RSA , 可以不传，不传则手机号解密直接使用AES解密
        String encryptType = "";
        // 免密登录后台url
        String privateKey = "";
        //创建应用时填入的rsa公钥对应的私钥字符串
        String flashLoginUrl = "";

        //获取客户端是安卓还是IOS 1:安卓 2：IOS
        Integer subBranch = branch / 100;
        if (subBranch == 1) {
            appId = ShanYanAndroidProps.appId;
            appKey = ShanYanAndroidProps.appKey;
            flashLoginUrl = ShanYanAndroidProps.flashLoginUrl;
            encryptType = ShanYanAndroidProps.encryptType;
            privateKey = ShanYanAndroidProps.privateKey;
        } else if (subBranch == 2) {
            appId = ShanYanIOSProps.appId;
            appKey = ShanYanIOSProps.appKey;
            flashLoginUrl = ShanYanIOSProps.flashLoginUrl;
            encryptType = ShanYanIOSProps.encryptType;
            privateKey = ShanYanIOSProps.privateKey;
        }

        //从SDK获取的token参数
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("token", token);
            params.put("appId", appId);
            params.put("encryptType", encryptType);//可以不传，不传则解密直接使用AES解密
            params.put("sign", SignUtils.getSign(params, appKey));
            JSONObject jsonObject = OkHttpUtil.postRequest(flashLoginUrl, params);
            if (null != jsonObject) {
                String code = jsonObject.getString("code");     //返回码 200000为成功
                String message = jsonObject.getString("message");//返回消息
                String chargeStatus = jsonObject.getString("chargeStatus"); //是否收费
                if ("200000".equals(code)) {
                    String dataStr = jsonObject.getString("data");
                    JSONObject dataObj = JSONObject.parseObject(dataStr);
                    String mobile = dataObj.getString("mobileName");
                    String tradeNo = dataObj.getString("tradeNo");//交易流水号
                    if ("0".equals(encryptType)) {
                        String key = MD5.getMD5Code(appKey);
                        mobile = AESUtils.decrypt(mobile, key.substring(0, 16), key.substring(16));
                    } else if ("1".equals(encryptType)) {
                        mobile = RSAUtils.decryptByPrivateKeyForLongStr(mobile, privateKey);
                    }
                    return mobile;
                } else {
                    log.info("创蓝闪验获取手机号码失败，请检验。获取置换手机号码的Token：" + jsonObject.toJSONString());
                }
            }
            return "";
        } catch (Exception e) {
            log.error("创蓝闪验获取手机号码失败，请检验。获取置换手机号码的Token：" + token, e);
            return "";
        }
    }
}
